package com.scfw.db;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by sehuaqi on 2017/7/2.
 */

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    IAccountService accountService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Account> getAccounts() {
        return accountService.findAccountList();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Account getAccountById(@PathVariable("id") int id) {
        return accountService.findAccountById(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String deleteAccountById(@PathVariable("id") int id) {
        int t = accountService.delete(id);
        if (t == 1) {
            return "succ";
        } else {
            return "fail";
        }

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateAccount(@PathVariable("id") int id,
                                @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "money", required = true) double money) {
        Account account = new Account();
        account.setMoney(money);
        account.setName(name);
        account.setId(id);
        int t = accountService.update(account);
        if (t == 1) {
            return JSON.toJSONString(account);
        } else {
            return "fail";
        }
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money) {
        Account account = new Account();
        account.setMoney(money);
        account.setName(name);
        int t = accountService.add(account);
        if (t == 1) {
            return JSON.toJSONString(account);
        } else {
            return "fail";
        }

    }

    /************ jpa test ************/

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public List<Account> findAll(){
        return accountService.findAll();
    }

    @RequestMapping(value = "/findOne/{id}", method = RequestMethod.GET)
    public Account findOne(@PathVariable("id") int id){

        return accountService.findOne(id);
    }

    @RequestMapping(value = "/updateAccount/{id}", method = RequestMethod.PUT)
    public String update(@PathVariable("id") int id,String name,double money){

        Account updateAccount = new Account();
        updateAccount.setMoney(money);
        updateAccount.setName(name);
        updateAccount.setId(id);

        return accountService.updateAccount(updateAccount);
    }

    @RequestMapping(value = "/save/", method = RequestMethod.POST)
    public String save(String name,double money){

        Account updateAccount = new Account();
        updateAccount.setMoney(money);
        updateAccount.setName(name);

        return accountService.save(updateAccount);
    }

    /************ mybatis test ************/

    @RequestMapping(value = "/m-findAll", method = RequestMethod.GET)
    public List<Account> mfindAll(){
        return accountService.mFindAll();
    }

    @RequestMapping(value = "/m-findOne/{id}", method = RequestMethod.GET)
    public Account mfindOne(@PathVariable("id") int id){

        return accountService.mFindOne(id);
    }

    @RequestMapping(value = "/m-xml-findOne/{id}", method = RequestMethod.GET)
    public Account mxmlfindOne(@PathVariable("id") int id){

        return accountService.mxmlFindOne(id);
    }

    @RequestMapping(value = "/m-updateAccount/{id}", method = RequestMethod.PUT)
    public int mUpdate(@PathVariable("id") int id,String name,double money){

        Account updateAccount = new Account();
        updateAccount.setMoney(money);
        updateAccount.setName(name);
        updateAccount.setId(id);

        return accountService.mUpdateAccount(updateAccount);
    }

    @RequestMapping(value = "/m-save", method = RequestMethod.POST)
    public int msave(String name,double money){

        Account updateAccount = new Account();
        updateAccount.setMoney(money);
        updateAccount.setName(name);

        return accountService.mSave(updateAccount);
    }

    @RequestMapping(value = "/transfer/{money}", method = RequestMethod.POST)
    public int transfer(@PathVariable double money){

        return accountService.transfer(money);
    }
}
